package com.example.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.reggie.common.CustomException;
import com.example.reggie.dao.SetmealDao;
import com.example.reggie.domain.Setmeal;
import com.example.reggie.domain.SetmealDish;
import com.example.reggie.dto.SetMealDto;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.SetmealDishService;
import com.example.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 23333
 * @description 针对表【setmeal(套餐)】的数据库操作Service实现
 * @createDate 2023-06-07 21:15:44
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal>
        implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveSetMealWithSetmealDishes(SetMealDto setMealDto) {
        List<SetmealDish> setmealDishes = setMealDto.getSetmealDishes();
        this.save(setMealDto);
        Long setMealDtoId = setMealDto.getId();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(String.valueOf(setMealDtoId));

        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据套餐id删除套餐及其对应的套餐菜品关联数据
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeWithSetmealDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId, ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus, 1);
        long count = this.count(setmealLambdaQueryWrapper);

        if (count > 0) {
            throw new CustomException("套餐未停售！无法删除");
        }

        this.removeBatchByIds(ids);

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeStatus(Integer status, List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> setmealLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        setmealLambdaUpdateWrapper.set(Setmeal::getStatus, status).in(Setmeal::getId, ids);
        this.update(setmealLambdaUpdateWrapper);
    }

    @Override
    public SetMealDto getSetmealDtoById(Long id) {
        Setmeal setmeal = this.getById(id);
        SetMealDto setMealDto = new SetMealDto();
        BeanUtils.copyProperties(setmeal, setMealDto);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishLambdaQueryWrapper);
        setMealDto.setSetmealDishes(setmealDishes);

        return setMealDto;
    }

    @Override
    public void updateSetmeal(SetMealDto setMealDto) {
        List<SetmealDish> setmealDishes = setMealDto.getSetmealDishes();
        Long id = setMealDto.getId();
        this.updateById(setMealDto);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(String.valueOf(id));
        }
        setmealDishService.saveBatch(setmealDishes);
    }

//    @Override
//    public Page<SetMealDto> page(Integer page, Integer pageSize, String name) {
//        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
//        Page<SetMealDto> setMealDtoPage = new Page<>();
//        BeanUtils.copyProperties(setmealPage, setMealDtoPage, "records");
//
//
//        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
//        slqw.like(name != null, Setmeal::getName, name);
//        this.page(setmealPage, slqw);
//
//        List<Setmeal> setmealPageRecords = setmealPage.getRecords();
//        List<SetMealDto> setMealDtoPageRecords = new ArrayList<>();
//
//
//        for (Setmeal setmealPageRecord : setmealPageRecords) {
//            SetMealDto setMealDto = new SetMealDto();
//            BeanUtils.copyProperties(setmealPageRecord, setMealDto);
//            Long categoryId = setmealPageRecord.getCategoryId();
//            Category category = categoryService.getById(categoryId);
//            String categoryName = category.getName();
//            setMealDto.setCategoryName(categoryName);
//            setMealDtoPageRecords.add(setMealDto);
//        }
//        setMealDtoPage.setRecords(setMealDtoPageRecords);
//
//        return setMealDtoPage;
//    }
}




